package cn.wtg.kotlinlib.SingleActivityMulFragmentUi

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import cn.wtg.kotlinlib.baseUi.BaseFragment
import cn.wtg.kotlinlib.baseViewModel.BaseLifecycleVM

/**
 * author: wtg
 * time: 2022/11/14
 * desc:
 */
abstract class BaseMVVMBackStackFragment<B : ViewDataBinding?, VM : BaseLifecycleVM> :
    BaseFragment() {
    protected var binding: B? = null
    protected var viewModelId = 0
    protected var viewModel: VM? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.e("create", "onCreateView")
        if (binding == null) {
            binding = DataBindingUtil.inflate(inflater, getLayoutRes(), container, false)
        }
        //view 创建之后
        viewModelId = getInitViewModelId()
        viewModel = getInitViewModel()
        binding?.setVariable(viewModelId,viewModel)
        lifecycle.addObserver(viewModel!!)
        initView(savedInstanceState, binding!!.root)
        Log.e("binding","$binding")
        return binding!!.root
    }

    /**
     * 获取viewModel
     *
     * @return viewModel
     */
    protected abstract fun getInitViewModel(): VM

    /**
     * 获取BR的id
     *
     * @return id
     */
    protected abstract fun getInitViewModelId(): Int
}